package com.zwxu.upload;

import com.amazonaws.services.s3.model.PartETag;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;


public class ResumableInfo {

    private int      resumableChunkSize;
    private long     resumableTotalSize;
    private String   resumableIdentifier;
    private String   resumableFilename;
    private String   resumableRelativePath;
    private String resumableFilePath;

    //s3 chunk start


    private String uploadId;
    //不断变化
    private int resumableCurrentChunkSize; //partSize
    private int resumableChunkNumber;
    //以下二者不在报文里面
    private List<PartETag> partETags = new ArrayList<PartETag>();//分段标记
    private HashSet<ResumableChunkNumber> uploadedChunks = new HashSet<ResumableChunkNumber>();

    public int getResumableChunkSize() {
        return resumableChunkSize;
    }

    public void setResumableChunkSize(int resumableChunkSize) {
        this.resumableChunkSize = resumableChunkSize;
    }

    public long getResumableTotalSize() {
        return resumableTotalSize;
    }

    public void setResumableTotalSize(long resumableTotalSize) {
        this.resumableTotalSize = resumableTotalSize;
    }

    public String getResumableIdentifier() {
        return resumableIdentifier;
    }

    public void setResumableIdentifier(String resumableIdentifier) {
        this.resumableIdentifier = resumableIdentifier;
    }

    public String getResumableFilename() {
        return resumableFilename;
    }

    public void setResumableFilename(String resumableFilename) {
        this.resumableFilename = resumableFilename;
    }

    public String getResumableRelativePath() {
        return resumableRelativePath;
    }

    public void setResumableRelativePath(String resumableRelativePath) {
        this.resumableRelativePath = resumableRelativePath;
    }

    public String getResumableFilePath() {
        return resumableFilePath;
    }

    public void setResumableFilePath(String resumableFilePath) {
        this.resumableFilePath = resumableFilePath;
    }

    public int getResumableChunkNumber() {
        return resumableChunkNumber;
    }

    public void setResumableChunkNumber(int resumableChunkNumber) {
        this.resumableChunkNumber = resumableChunkNumber;
    }

    public int getResumableCurrentChunkSize() {
        return resumableCurrentChunkSize;
    }

    public void setResumableCurrentChunkSize(int resumableCurrentChunkSize) {
        this.resumableCurrentChunkSize = resumableCurrentChunkSize;
    }

    public String getUploadId() {
        return uploadId;
    }

    public void setUploadId(String uploadId) {
        this.uploadId = uploadId;
    }

    public List<PartETag> getPartETags() {
        return partETags;
    }

    public void setPartETags(List<PartETag> partETags) {
        this.partETags = partETags;
    }

    public HashSet<ResumableChunkNumber> getUploadedChunks() {
        return uploadedChunks;
    }

    public void setUploadedChunks(HashSet<ResumableChunkNumber> uploadedChunks) {
        this.uploadedChunks = uploadedChunks;
    }

    public void addPartETag(PartETag partETag) { //NOPMD
        partETags.add(partETag);
    }
    /**
     * 同步锁添加上传成功的片段
     * @param c
     */
    public void addUploadedChunks(ResumableChunkNumber c) { //NOPMD
        uploadedChunks.add(c);
    }


    //s3 chunk end

    public static class ResumableChunkNumber {
        public ResumableChunkNumber(int number) {
            this.number = number;
        }

        public int number;

        @Override
        public boolean equals(Object obj) {
            return obj instanceof ResumableChunkNumber
                    ? ((ResumableChunkNumber)obj).number == this.number : false;
        }

        @Override
        public int hashCode() {
            return number;
        }
    }

    public boolean checkIfUploadFinished() {
        //check if upload finished
        int count = (int) Math.ceil(((double) resumableTotalSize) / ((double) resumableChunkSize));
        for(int i = 1; i < count; i ++) {
            if (!uploadedChunks.contains(new ResumableChunkNumber(i))) {
                return false;
            }
        }
        //Upload finished, change filename.
        File file = new File(resumableFilePath);
        String new_path = file.getAbsolutePath().substring(0, file.getAbsolutePath().length() - ".temp".length());
        file.renameTo(new File(new_path));
        return true;
    }

    public String toString(){
        return "resumableChunkSize:" + resumableChunkSize +
                "  resumableTotalSize:" + resumableTotalSize +
                "  resumableIdentifier:" + resumableIdentifier;
    }
}
